package leetcode_2301_2400;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class LeeCode_2352 {
    public static void main(String[] args) {

    }
    private static int equalPairs(int[][] grid) {
        Map<String, Integer> m1 = new HashMap<>();
        Map<String, Integer> m2 = new HashMap<>();
        int n = grid.length;
        for (int[] ints : grid) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < n; j++) {
                sb.append(ints[j]);
                sb.append(",");
            }
            String key = sb.toString();
            m1.put(key, m1.getOrDefault(key, 0) + 1);
        }
        for (int j = 0; j < n; j++) {
            StringBuilder sb = new StringBuilder();
            for (int[] ints : grid) {
                sb.append(ints[j]);
                sb.append(",");
            }
            String key = sb.toString();
            m2.put(key, m2.getOrDefault(key, 0) + 1);
        }
        int ans = 0;
        for (String k1 : m1.keySet()) {
            if (m2.containsKey(k1)){
                ans += m1.get(k1) * m2.get(k1);
            }
        }
        return ans;
    }
}
